package com.cqcet.wenchuang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqcet.wenchuang.common.R;
import com.cqcet.wenchuang.dto.GoodsDto;
import com.cqcet.wenchuang.entity.Admin;
import com.cqcet.wenchuang.entity.Goods;
import com.cqcet.wenchuang.service.AdminService;
import com.cqcet.wenchuang.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品处理
 */
import com.cqcet.wenchuang.common.R;
import com.cqcet.wenchuang.entity.Goods;
import com.cqcet.wenchuang.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 商品处理
 */

@Api(tags = "商品管理接口")
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private AdminService adminService;

    /**
     * 新增商品
     *
     * @param goods
     * @return
     */
    @ApiOperation(value = "新增商品", notes = "必要:adminId,name,imageUrl,description,quantity,price,categoryId")
    @PostMapping
    public R<String> save(@RequestBody Goods goods) {
        goodsService.save(goods);
        return R.success("添加成功");
    }

    /**
     * 删除商品
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除", notes = "必要:id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, paramType = "path"),
    })
    @DeleteMapping("/{id}")
    public R<String> remove(@PathVariable Long id) {
        goodsService.removeById(id);
        return R.success("删除成功");
    }

    /**
     * 修改商品
     *
     * @param goods
     * @return
     */
    @ApiOperation(value = "修改商品", notes = "必要:id,可选:name,imageUrl,description,quantity,price,categoryId")
    @PutMapping
    public R<String> update(@RequestBody Goods goods) {
        goodsService.updateById(goods);
        return R.success("修改成功");
    }

    /**
     * 根据id查询商品
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询", notes = "必要:id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, paramType = "path"),
    })
    @GetMapping("/{id}")
    public R<GoodsDto> getById(@PathVariable Long id) {
        GoodsDto goodsDto = new GoodsDto();

        Goods goods = goodsService.getById(id);
        //将商品信息拷贝至dto对象
        BeanUtils.copyProperties(goods, goodsDto);
        //查询商家信息
        Admin admin = adminService.getById(goods.getAdminId());

        goodsDto.setAdmin(admin);
        return R.success(goodsDto);
    }

    /**
     * 商品列表
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @ApiOperation(value = "商品分页", notes = "商家&管理员使用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页多少", required = true),
            @ApiImplicitParam(name = "name", value = "商品名"),
            @ApiImplicitParam(name = "status", value = "状态"),
    })
    @GetMapping("/page")
    public R<Page<Goods>> page(Integer page, Integer pageSize, String name, Integer status) {
        Page<Goods> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        //根据名字模糊查询
        queryWrapper.like(StringUtils.isNotBlank(name), Goods::getName, name);
        //根据状态查询
        queryWrapper.eq(ObjectUtils.isNotEmpty(status), Goods::getStatus, status);
        //根据更新时间降序排序
        queryWrapper.orderByDesc(Goods::getUpdateTime);
        goodsService.page(pageInfo, queryWrapper);

        return R.success(pageInfo);
    }

    /**
     * 条件查询
     *
     * @return
     */
    @ApiOperation(value = "条件查询", notes = "可选:name,status,categoryId,lessThen,moreThen")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lessThen", value = "商品价格少于"),
            @ApiImplicitParam(name = "moreThen", value = "商品价格多于"),
    })
    @GetMapping("/list")
    public R<List<Goods>> list(GoodsDto goodsDto) {
        List<Goods> goodsList = goodsService.goodsList(goodsDto);
        return R.success(goodsList);
    }

    /**
     * 删除商品
     * @param goods
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestBody Goods goods) {
        goods.setStatus(0);
        goodsService.updateById(goods);
        return R.success("删除成功");
    }
}
